<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class XiaTianController extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            case 1:
            case 2:
                $action = 'USao';
                break;
            case 3:
            case 4:
                $action = 'AliSao';
                break;
            case 17:
            case 18:
                $action = 'USao';
                break;
            default:
                $this->showmessage('不支持此产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $Md5key = $this->config['signkey'];  //密钥

        $native = array(
            'action' => $action,
            'txnamt' => $return['amount']*100,
            'merid' => $this->config['mch_id'],
            'orderid' => $return['orderid'],
            'backurl' => $return['notifyurl'],
            'ip' => getIP(),
        );

        //echo "<pre/>"; print_r($native); exit;

        //格式化json字符串
        $jsonData=json_encode($native);
        //输出Base64字符串
        $base64Data = base64_encode($jsonData);
        //拼接待签名字符
        $signData = $base64Data.$Md5key;
        //签名
        $sign = md5($signData);
        //拼接请求参数
        $req = $base64Data;

        //发送请求得到结果
        $requestData = [
            'req' => $req,
            'sign'=>$sign,
        ];

        $res = $origin = curl($this->config['gateway'],httpstr($requestData));
        $result =  json_decode($res,true);
        $resp = json_decode(base64_decode($result['resp']),true);

        $code = $resp['formaction'];

        if(!$code){
            if($resp['respmsg']){
                $this->showmessage($resp['respmsg'], ['code' => C("THIRDCODES." . $this->class)]);
            }

            $this->showmessage('未获取到支付链接!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $ismobile = isMobile();

        $paytype = transArr( C("PAYTYPES"), "id");
        $this->assign("wap_url", $code);
        //$this->assign("code_url", $res['data']['payurl']);
        $this->assign("title", $paytype[$this->config['paytype']]);
        $this->assign("return", $return);
        $this->assign("imgurl", C("website.DOMAIN")."/Pay_Qrcode_getCode?type=2&data=" . base64_encode($code));

        switch($this->config['paytype']){
            case 1:
            case 2:
                if($ismobile){
                    $this->display("WeiXin/weixinwap");
                }else{
                    $this->display("WeiXin/weixin");
                }
                break;
            //支付宝扫码
            case 3:
                //支付宝H5
            case 4:
                header("Location: {$code}");
                break;
            case 17:
            case 18:
                $this->assign("deny_layer",1);
                if($ismobile){
                    $this->display("WeiXin/alipaywap");
                }else{
                    $this->display("WeiXin/alipay");
                }
                break;
            default:
                $this->display("WeiXin/Pay");
        }
    }

    //通知
    public function notifyurl(){
        //$this->debug_callback();
        if(!$this->checkip(getIP(),['120.25.215.97','120.77.13.164'])){
            exit("wrong");
        }
        $res = I("post.");

        $p = json_decode(base64_decode($res['resp']),true);

        //获取订单信息
        $orderid = $p['orderid'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$orderid))->save(array(

            "pay_orderid_rd"=>htmlspecialchars($p['queryid'])));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();

        $Md5key = $this->config['signkey'];

        //拼接待签名字符
        $signData = $res['resp'].$Md5key;
        //签名
        $sign = md5($signData);

        $sign2 = $res['sign'];
        if($sign !== $sign2){
            exit("sign_wrong");
        }
        if($p['resultcode'] !== '0000'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'0000');
    }

    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}